DEFINIÇÃO
O Leaflet é uma biblioteca JavaScript (JS) conhecida pela criação de excelentes mapas interativos. O pacote “leaflet” do R recebe o mesmo da biblioteca em JavaScript e é um facilitador da interação entre o R e as funções escritas em JS.
PRINCIPAIS FUNCIONALIDADES
PRINCIPAIS VANTAGENS
leaflet() %>%
addTiles() %>%
addMarkers(lng = 174.768,
lat = -36.852,
popup = "Local de Nascimento do R")CONHECIMENTOS ADQUIRIDOS
addTiles()
addMarkers()
leaflet() %>%
addProviderTiles("CartoDB") %>%
addMarkers(lng = 174.768, lat = -36.852, popup = "Local de Nascimento do R") %>%
addCircleMarkers(lng = 174.778, lat = -36.854,
radius = 10,
color = "red",
label = "Próximo ao local de Nascimento do R")CONHECIMENTOS ADQUIRIDOS
addProviderTiles()
addCircleMarkers()
Label vs. Popup
leaflet(options = leafletOptions(minZoom = 10, dragging = TRUE)) %>%
addTiles() %>%
addMarkers(lng = 174.768, lat = -36.852, popup = "Local de Nascimento do R") %>%
setView(lng = 174.8,
lat = -36.9,
zoom = 10)CONHECIMENTOS ADQUIRIDOS
Zoom permitido ao usuário
setView()
#### Referenciando as colunas sem passar a data frame através do pipe:
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = tabela_100_escolas_mirow$ESCOLA_GEO_LONGITUDE,
lat = tabela_100_escolas_mirow$ESCOLA_GEO_LATITUDE)
#### Referenciando as colunas passando a data frame através do pipe:
tabela_100_escolas_mirow %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = ~ESCOLA_GEO_LONGITUDE,
lat = ~ESCOLA_GEO_LATITUDE)CONHECIMENTOS ADQUIRIDOS
Quando usar cada forma?
tabela_100_escolas_mirow %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = ~ESCOLA_GEO_LONGITUDE,
lat = ~ESCOLA_GEO_LATITUDE,
group = "escolas",
label = ~ESCOLA_CD_CENSO) %>%
addSearchOSM()O QUE FOI FEITO?
CONHECIMENTOS ADQUIRIDOS
Ferramenta Buscador
Labels
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = tabela_100_escolas_mirow$ESCOLA_GEO_LONGITUDE,
lat = tabela_100_escolas_mirow$ESCOLA_GEO_LATITUDE,
group = "mirow",
color = "red",
label = paste0("Mirow - ", tabela_100_escolas_mirow$ESCOLA_CD_CENSO)) %>%
addCircleMarkers(lng = tabela_100_escolas_censo$NU_LONGITUDE,
lat = tabela_100_escolas_censo$NU_LATITUDE,
group = "censo",
color = "blue",
label = paste0("Censo - ", tabela_100_escolas_censo$CO_ENTIDADE)) %>%
addLayersControl(overlayGroups = c("mirow", "censo"))O QUE FOI FEITO?
CONHECIMENTOS ADQUIRIDOS
addLayersControl()
tabela_escolas_mirow %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = ~ESCOLA_GEO_LONGITUDE, lat = ~ESCOLA_GEO_LATITUDE,
group = "mirow", color = "red", label = paste0("Mirow - ", ~ESCOLA_CD_CENSO),
clusterOptions = markerClusterOptions())O QUE FOI FEITO?
CONHECIMENTOS ADQUIRIDOS
clusterOptions = markerClusterOptions()
tabela_escolas_mirow %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = ~ESCOLA_GEO_LONGITUDE, lat = ~ESCOLA_GEO_LATITUDE,
group = "mirow",
color = "red",
label = paste0("Mirow - ", ~ESCOLA_CD_CENSO),
clusterOptions = markerClusterOptions()) %>%
addPolygons(data = shape_regionais,
weight = 2,
opacity = 0.2,
color = "orange",
dashArray = "3",
fillOpacity = 0.2,
highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE))O QUE FOI FEITO?
CONHECIMENTOS ADQUIRIDOS
addPolygons()
### Seleção de uma escola para plotagem
escola_selecionada <- 31000281
### Todos os pontos de destino - localização dos alunos
dest_df <- tabela_alunos_mirow %>%
filter(ESCOLA_CD_CENSO == escola_selecionada) %>% select(ALUNO_GEO_LONGITUDE, ALUNO_GEO_LATITUDE) %>%
setNames(c("ESCOLA_GEO_LONGITUDE", "ESCOLA_GEO_LATITUDE"))
### Latitude isolada da Escola:
orig_lat <- tabela_escolas_mirow %>%
filter(ESCOLA_CD_CENSO == escola_selecionada) %>% pull(ESCOLA_GEO_LATITUDE) %>% as.numeric()
### Longitude isolada da Escola:
orig_long <- tabela_escolas_mirow %>%
filter(ESCOLA_CD_CENSO == escola_selecionada) %>% pull(ESCOLA_GEO_LONGITUDE) %>% as.numeric()
#### Fazer com que as coordenadas da Escolas sejam X vezes a quantidade de alunos:
orig_df <- data.frame(ESCOLA_GEO_LATITUDE = c(rep.int(orig_lat, nrow(dest_df))),
ESCOLA_GEO_LONGITUDE = c(rep.int(orig_long,nrow(dest_df)))) %>% as_tibble()O QUE FOI FEITO?
### Criar uma sequência intercalada de: escola-aluno, escola-aluno, ...
orig_df$sequence <- c(sequence = seq(1, length.out = nrow(orig_df), by=2))
dest_df$sequence <- c(sequence = seq(2, length.out = nrow(orig_df), by=2))
### Juntar a dataFrame final ordenada pela sequência criada.
tabela_linhas <- orig_df %>%
select(ESCOLA_GEO_LATITUDE, ESCOLA_GEO_LONGITUDE, sequence) %>%
bind_rows(dest_df) %>% arrange(sequence)O QUE FOI FEITO?
CONHECIMENTOS ADQUIRIDOS
addPolylines()